package org.databasefacade.xml.schema.tags;


import java.util.ArrayList;

import org.databasefacade.LogManager;
import org.databasefacade.registry.TableRegistry;
import org.databasefacade.util.XMLDataRecord;
import org.databasefacade.util.XMLDataRecordList;
import org.databasefacade.util.XMLParseUtil;
import org.databasefacade.xml.schema.XMLSchemaConst;


public class ColumnsTagAssign implements XMLSchemaConst
{
	static XMLParseUtil xmlutil = new XMLParseUtil();
	
	public static void assign(TableRegistry registry, XMLDataRecord table)
	{
		processColumns(table);
	}
	
	private static void processColumns(XMLDataRecord table) {
		ArrayList tagHierarchy = new ArrayList();
		tagHierarchy.add(TAG_COLUMNS);
		tagHierarchy.add(TAG_COLUMN);
		XMLDataRecordList columns = new XMLDataRecordList();
		xmlutil.assignTagXMLData(columns, tagHierarchy, table.getChildNodes());
		LogManager.info(columns.toString());
		if ( columns.size() == 0 ) {
			throw new RuntimeException(
				"There must be atleast one <" + TAG_COLUMN + "> element per table");
		}
		for ( int i = 0; i < columns.size(); i++ ) {
			XMLDataRecord column = columns.get(i);
			processColumnDescription(column);
			String colName = column.getAttributeValue(TAG_COLUMN_ATTR_NAME);
			if ( colName == null ) {
				throw new RuntimeException(
					"The <" + TAG_COLUMN + "> element must have a '" + TAG_COLUMN_ATTR_NAME
					+ "' attribute");
			}
			System.out.println(colName);
		}	
	}
	
	private static void processColumnDescription(XMLDataRecord column) {
		ArrayList tagHierarchy = new ArrayList();
		tagHierarchy.add(TAG_COLUMN_DESCRIPTION);
		XMLDataRecordList description = new XMLDataRecordList();
		xmlutil.assignTagXMLData(description, tagHierarchy, column.getChildNodes());
		LogManager.info(description.toString());
		if ( description.size() == 1 ) {
        	XMLDataRecord rec = description.get(0);
			System.out.println(rec.getValue());
		}
	}
}
